# -*- coding: utf-8 -*-
# $Id: models.py 51 2009-03-13 11:04:33Z glitch $

from django.db import models
from django.utils.translation import ugettext_lazy as _

label = _('Log')

severity_names = (('emergency', 'system is unusable'),
                  ('alert', 'action must be taken immediately'),
                  ('critical', 'critical conditions'),
                  ('error', 'error conditions'),
                  ('warning', 'warning conditions'),
                  ('notice', 'normal but significant condition'),
                  ('informational', 'informational messages'),
                  ('debug', 'debug-level messages'))

facility_names = (('kernel', 'kernel messages'),
                  ('user', 'user-level messages'),
                  ('mail', 'mail system'),
                  ('daemon', 'system daemons'),
                  ('security', 'security/authorization messages'),
                  ('internal', 'messages generated internally by syslogd'),
                  ('lpr', 'line printer subsystem'),
                  ('news', 'network news subsystem'),
                  ('ppp', 'UUCP subsystem'),
                  ('time', 'clock daemon'),
                  ('security', 'security/authorization messages'),
                  ('ftp', 'FTP daemon'),
                  ('time', 'NTP subsystem'),
                  ('audit', 'log audit'),
                  ('alert', 'log alert'),
                  ('time', 'clock daemon'),
                  ('local0', 'local use 0'),
                  ('local1', 'local use 1'),
                  ('local2', 'local use 2'),
                  ('local3', 'local use 3'),
                  ('local4', 'local use 4'),
                  ('local5', 'local use 5'),
                  ('local6', 'local use 6'),
                  ('local7', 'local use 7'))

class Event(models.Model):
    u""" Запись в журнале. """
    date = models.DateField(_('date'), db_index=True)
    time = models.TimeField(_('time'), db_index=True)
    host = models.CharField(_('host'), max_length=32, db_index=True)
    program = models.CharField(_('program'), max_length=64,
                               db_index=True, blank=True)
    pid = models.IntegerField(_('pid'), null=True)
    severity = models.PositiveSmallIntegerField(_('severity'), db_index=True)
    facility = models.PositiveSmallIntegerField(_('facility'), db_index=True)
    message = models.TextField(_('message'), db_index=True)
    
    def __str__(self):
        return '%s (%s): %s' % (self.time, self.program, self.message)
    
    def _get_severity_name(self):
        return severity_names[self.severity][0]

    def _get_facility_name(self):
        return facility_names[self.facility][0]
    
    severity_name = property(_get_severity_name)
    
    class Meta:
        verbose_name = _('Event')
        verbose_name_plural = _('Events')
        ordering = ('-date', '-time', 'severity', 'facility', 'host')
